#include<stdio.h>
#include<string.h>
#include<unistd.h>
//#include""processbar.h"
#define Style '*'
#define Length 101
const char *lable = "\\|/-";
//void ProcessBar()
//{
//    char bar[Length];
//    memset(bar,'\0',sizeof(bar));
//    int len = strlen(lable);
//    int cnt = 0;
//    while(cnt <= 100)
//    {
//    printf("[%-100s][%d%%][%c]\r",bar,cnt,lable[cnt%len]);
//      fflush(stdout);
//      bar[cnt] = Style;
//      cnt++;
//      usleep(2000);
//    }
//    printf("\n");
//}
//int main()
//{
 // ProcessBar();
  ////倒计时程序
//  int cnt = 10;
//  while(cnt)
//  {
//    printf("%倒计时：2d\r",cnt);
//    fflush(stdout);
//    cnt--;
//    //如果不休眠，会导致刷新缓冲区太快，显示器会看不到效果
//    //sleep(1);
 // }
//  return 0;
//}
typedef void (*callback_t)(double,double);
double bandwidth = 1024*1024;
void ProcessBar(double filesize,double current)
{
  char bar[101] = {0};
  memset(bar,'\0',sizeof(bar));
  //循环次数
  double rate = ( current*100.0) / filesize;
  int cnt = 0;
  int loop_count = (int)rate;
  int len = strlen(lable);
  while(cnt <= loop_count)
  {
  //usleep(2000);
  bar[cnt++] = Style;
  }
  printf("[%-100s][%.1f%%][%c]\r",bar,rate,lable[cnt%4]);
  fflush(stdout);
  usleep(2000);
}
void download(double filesize,callback_t cb)
{
  printf("开始下载\n");
  double current = 0.0;
  while(current <= filesize)
  {
    cb(filesize,current);
    usleep(2000);
    current+=bandwidth;
  }
  printf("\n下载结束\n");
}
int main()
{
  download(1024*1024*100,ProcessBar);
  return 0;

}
